#include "CollisionHandler.h"
#include "sprite.h"
CollisionHandler* CollisionHandler::m_pInstance = nullptr;

void CollisionHandler::attachObserver(Sprite* o) {
	m_observers.push_back(o);
}

void CollisionHandler::detachObserver(Sprite* o) {
    m_observers.erase(
        std::remove(m_observers.begin(), m_observers.end(), o),
        m_observers.end()
    );
}

void CollisionHandler::detachAll() {
    m_observers.clear();
}

bool CollisionHandler::checkCollision(Sprite* o) {
    bool result = false;
    int ax, ay, aw, ah;
    int bx, by, bw, bh;
    bx = o->GetX();
    by = o->GetY();
    bw = o->GetWidth();
    bh = o->GetHeight();

    for (Sprite* s : m_observers) {
        ax = s->GetX();
        ay = s->GetY();
        aw = s->GetWidth();
        ah = s->GetHeight();

        if (ax<bx + bw && ax + aw>bx && ay<by + bh && ay + ah>by)
        {
            result = true;
            s->remove = true;
        }
    }
    return result;
}
